//
//  TestBaseProtocol.swift
//  TestSwift
//
//  Created by 小星星吃KFC on 2021/6/1.
//

import Foundation


public struct AXAnyBase<Base> {
    private let base: Base
    public init(_ base: Base) {
        self.base = base
    }
}


protocol AXAnyBaseProtocol {
    // protocol中 需要用 associatedtype 来预设一个类型
    associatedtype T
    
    var ax: T { get }
    
    static var ax: T.Type { get }
    
}

extension AXAnyBaseProtocol {
    var ax: AXAnyBase<Self> {
        return AXAnyBase<Self>(self)
    }
    
    static var ax: AXAnyBase<Self>.Type {
        return AXAnyBase<Self>.self
    }
}

/// Extend NSObject with `rx` proxy.
//extension Iterator: AXAnyBaseProtocol { }
//extension NSObject: AXAnyBaseProtocol { }

extension String: AXAnyBaseProtocol {}
/// AnyOne:是 UIImageView类型
/// where Base 表示这个泛型是不同情况
extension AXAnyBase where Base == String {
    /// url 含有中文编码
    /// - Returns: String
    func encoding() -> String {
        return self.base.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! // iOS 9之后。
    }
    
    func decoding() -> String {
        
        return self.base.removingPercentEncoding! // iOS 9之后。
    }
    
    
}


func TestBaseProtocol() {
    
    print( "https://www.google.com/search?q=成都市".ax.encoding())
    
    print("https://www.google.com/search?q=成都市".ax.encoding().ax.decoding())
    
    let str1:String = String.ax.encoding(AXAnyBase<String>("https://www.google.com/search?q=成都市"))()
    
    print(str1)
    
    
}
